package com.haiqi.modules.message.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.Message;
import com.haiqi.common.persistence.entity.ShipInfo;
import com.haiqi.common.utils.DateUtils;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.message.service.MessageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hythzx on 2016/6/21.
 */
@Controller
@RequestMapping("/manage/message")
@SafeAuthority(menuCode = "messageManage,monitorLocation")
public class MessageController extends BaseController{

    private String menuCode= "";
    
    @Autowired
    private MessageService messageService;

    @RequestMapping("/list")
    public String list(Message message, PageBean pageBean, HttpSession session, Model model,String key){
        ResponseBean.UserBean user = getUser(session);
        ShipInfo shipInfo=new ShipInfo();
        shipInfo.setShipName(key);
        message.setCompany(user.getOfficeid());
        message.setShipInfo(shipInfo);
        message.setTitle(key);
        menuCode="outBox";
        generateMenuCode(session, menuCode);
        PageInfo<Message> pageInfo = messageService.getDataList(message,pageBean);
        model.addAttribute("param",message);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("key",key);
        return "/modules/message/list";
    }

//    @RequestMapping("/historyList")
//    public String historyList(Message message, PageBean pageBean, HttpSession session, Model model,String key){
//        ResponseBean.UserBean user = getUser(session);
//        ShipInfo shipInfo=new ShipInfo();
//        shipInfo.setShipName(key);
//        message.setCompany(user.getOfficeid());
//        message.setMsgstate("1");
//        message.setShipInfo(shipInfo);
//        message.setTitle(key);
//        menuCode="historyMessage";
//        generateMenuCode(session, menuCode);
//        PageInfo<Message> pageInfo = messageService.getDataList(message,pageBean);
//        model.addAttribute("param",message);
//        model.addAttribute("pageInfo",pageInfo);
//        model.addAttribute("key",key);
//        return "/modules/message/historyList";
//    }


    @RequestMapping("/ajax/list")
    @ResponseBody
    public DataGridBean<Message>  getJsonList(Message message,String title,String stime,String etime,PageBean pageBean, HttpSession session){
        ResponseBean.UserBean user = getUser(session);
        message.setCompany(user.getOfficeid());
        PageInfo<Message> pageInfo = messageService.getDataList(message, pageBean);
        DataGridBean<Message> dataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo, dataGridBean);
        return dataGridBean;
    }



    @RequestMapping("/detail")
    @ResponseBody
    public Message getShipInfo(Integer id){
        Message message = messageService.getMessageInfo(id);
        return message;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map updateShipInfo(Message message, HttpSession session){
        ResponseBean.UserBean user = getUser(session);
        if(StringUtils.isNotEmpty(user.getName())){
            message.setMsgauthor(user.getName());
        }else{
            message.setMsgauthor(user.getLoginname());
        }
        message.setCompany(user.getOfficeid());
        String date = DateUtils.dateToString(new Date(),DateUtils.DATETIME_FORMAT);
        message.setSendtime(date);
        Map<String,Boolean> response = new HashMap<>();
        boolean success = messageService.update(message);
        response.put("success",success);
        return response;
    }
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(String id){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = messageService.deleteArray(id);
        response.put("success",success);
        return response;
    }
    
}
